/*
Source : https://leetcode.com/problems/valid-palindrome/
Author : nflush@outlook.com
Date   : 2016-07-04
*/

/*
Total Accepted: 107804 Total Submissions: 445123 Difficulty: Easy

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

Subscribe to see which companies asked this question
*/

class Solution {
private:
     char lowerChar(char b){
        if (b >= 'a' && b <= 'z'){
            return b;
        }
        if (b >= 'A' && b <= 'Z'){
            return b - 'A' + 'a';
        }
        if (b >= '0' && b <= '9'){
            return b;
        }
        return 0;
    }
public:
    bool isPalindrome(string s) {
        int h= -1;
        int e= s.size();
        while(e>h){
            ++h;
            --e;
            while (0 == lowerChar(s[e]) && e>h){
                --e;
            }
            while (0 == lowerChar(s[h]) && e>h){
                ++h;
            }
            if (lowerChar(s[h]) != lowerChar(s[e])){
                return false;
            }
        }
        return lowerChar(s[h]) == lowerChar(s[e]);
    }
};